﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MVCManage.Api.Models;
using MVCManage.BLL;
using MVCManage.Model;
using Newtonsoft.Json.Linq;
namespace MVCManage.Api.Controllers
{
    public class MessageController : ApiController
    {
        MessageBLL bll = new MessageBLL();
        // GET: api/Message
        public ResultPageList<MessageDto> GetMessagesPagelist(int page,int size) {
            int count = 0;
            var list = from n in bll.GetMessages(page, size, out count)
                       select new MessageDto()
                       {
                           Id= n.Id,
                            Address= n.Address,
                             Content= n.Content,
                              Time= n.Time,
                               Uid=n.Users.userID,
                              Title= n.Title
                       };
            ResultPageList<MessageDto> result = new ResultPageList<MessageDto>();
            result.code = 1;
            result.msg = "ok";
            result.count= count;
            result.data = list.ToList();
            return result;

        }
        // POST: api/Message
        public Result Post([FromBody]Message value)
        {
            value.Time= DateTime.Now;
            var pd = bll.Add(value);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "添加成功" : "添加失败";
            return result;

        }

        // PUT: api/Message/5
        

        // DELETE: api/Message/5
        public Result Delete(int id)
        {
            var pd = bll.Delete(id);
            Result result = new Result();
            result.code = pd ? 1 : 0;
            result.msg = pd ? "删除成功" : "删除失败";
            return result;
        }
    }
}
